package net.jsoft.pms.job.problem;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.Context;

import net.jsoft.platform.core.util.spring.SpringUtil;
import net.jsoft.platform.mail.service.MailService;
import net.jsoft.platform.schedule.job.BaseJob;
import net.jsoft.platform.security.entity.user.SysUser;
import net.jsoft.pms.entity.dept.Dept;
import net.jsoft.pms.entity.problem.ProblemTask;
import net.jsoft.pms.mapperwtglpt.problems.ProblemMapper;
import net.jsoft.pms.service.mysql.problem.IProblemService;

@Component
public class GetProblemJobFromDB extends BaseJob
{

    @Override
    public void doExecute(JobExecutionContext context) throws JobExecutionException
    {

        try
        {
            ProblemMapper mapper = SpringUtil.getBean("problemMapper", ProblemMapper.class);
            List<Map<String, Object>> list = mapper.findProblems();
            IProblemService service = SpringUtil.getBean("problemServiceImpl", IProblemService.class);
            //新增问题
            List<ProblemTask> psList = service.batchImport(list);
            //问题分类 按照人员分配
            Map<SysUser, List<ProblemTask>> addMap = new HashMap<SysUser, List<ProblemTask>>();

            //问题分类 按照组别分配
            Map<Dept, List<ProblemTask>> addOrgMap = new HashMap<Dept, List<ProblemTask>>();
            List<ProblemTask> ls = null;
            for (ProblemTask p : psList)
            {
                ls = addMap.get(p.getOptPerson());
                if (ls == null)
                {
                    ls = new ArrayList<ProblemTask>();
                }
                ls.add(p);
                addMap.put(p.getOptPerson(), ls);

                ls = addOrgMap.get(p.getDept());
                if (ls == null)
                {
                    ls = new ArrayList<ProblemTask>();
                }
                ls.add(p);
                addOrgMap.put(p.getDept(), ls);
            }

            MailService mailService = SpringUtil.getBean("mailService", MailService.class);

            TemplateEngine templateEngine = SpringUtil.getBean("templateEngine", TemplateEngine.class);

            JdbcTemplate jdbcTemplate = SpringUtil.getBean("mysqlJdbcTemplate", JdbcTemplate.class);

            //给组员发送邮件
            for (SysUser u : addMap.keySet())
            {
                Context cnt = new Context();
                cnt.setVariable("message", addMap.get(u));
                cnt.setVariable("name", u.getFullName());
                String message = templateEngine.process("pms/mysql/mail/newProblemByUser", cnt);
                //                mailService.sendHtmlMail(new String[] {u.getEmail()} , "问题管理平台新增问题提示", message);
                //                System.out.println(u.getEmail());
                //                System.out.println(message);
                mailService.sendHtmlMail(new String[]
                { u.getEmail()}, "问题管理平台新增问题提示", message);
            }

            //给组长发送邮件
            for (Dept dept : addOrgMap.keySet())
            {
                String sql = "select u.fullName , u.email from t_security_user u join t_security_user_role ur on u.id = ur.user_Id  and u.dept = ? join t_security_role r on ur.role_id = r.id and r.name = 'ROLE_GROUPLEADER'";
                List<Map<String, Object>> tempMap = jdbcTemplate.queryForList(sql, dept.getId());
                String n = "";

                String[] e = new String[tempMap.size()];

                int i = 0;
                for (Map<String, Object> map : tempMap)
                {
                    n += map.get("fullName").toString() + "  ";
                    e[i] = map.get("email").toString();
                    i++;
                }

                Context cnt = new Context();
                cnt.setVariable("message", addOrgMap.get(dept));

                cnt.setVariable("name", n);
                String message = templateEngine.process("pms/mysql/mail/newProblemByDept", cnt);
                //                System.out.println(message);
                //                mailService.sendHtmlMail(u.getEmail(), "问题管理平台新增问题提示", message);
                //                System.out.println(e);
                mailService.sendHtmlMail(e, "组内问题管理平台新增问题提示", message);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

}
